package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.dataConfig.dataResult;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.pojo.TbUserScoreAccount;
import com.dongdongshop.pojo.TbUserScoreDetail;
import com.dongdongshop.pojo.TbUserScoreUse;
import com.dongdongshop.service.ScoreAccountService;
import com.dongdongshop.service.ScoreDetailService;
import com.dongdongshop.service.ScoreUseService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("score")
public class ScoreController {
    private Logger logger = LoggerFactory.getLogger(ScoreController.class);
    @Reference
    private ScoreAccountService scoreAccountService;
    @Reference
    private ScoreDetailService scoreDetailService;
    @Reference
    private ScoreUseService scoreUseService;

    @RequestMapping("userScore")

    @ResponseBody
    public Integer  userScore(Long score){
        TbUser user =  (TbUser)SecurityUtils.getSubject().getPrincipal();
        String username = user.getUsername();
        TbUserScoreAccount userAccount = scoreAccountService.getByUserId(username);
        Date date = new Date();
        Integer  result = 0;
        if (userAccount.getTotalScore()>score){
            /*修改积分账号表*/
            userAccount.setTotalScore(userAccount.getTotalScore()-score);
            userAccount.setUpdatetime(new Date());
            userAccount.setExpendScore(userAccount.getExpendScore()+score);
            scoreAccountService.updateinfo(userAccount);
            logger.info("积分账号表修改完成");

            /*积分流水表*/
            TbUserScoreDetail tbUserScoreDetail = new TbUserScoreDetail();
            tbUserScoreDetail.setUserId(username);
            tbUserScoreDetail.setScore(-score);
            tbUserScoreDetail.setAddtime(date);
            tbUserScoreDetail.setNote("使用积分");
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DATE,7);
            Date closeTime = c.getTime();
            tbUserScoreDetail.setClosetime(closeTime);
            scoreDetailService.toAdd(tbUserScoreDetail);
            logger.info("积分流水表修改完成");

            /*修改可用积分表*/
            List<TbUserScoreUse> scoreUses = scoreUseService.getByName(username);
            if (scoreUses.size()>0){
                List<Integer> ilist = new ArrayList<>();//接收id
                TbUserScoreUse tbUserScoreUse = null;//接收最后一条记录
                Long sumScore = 0L;
                for (int i = 0; i < scoreUses.size(); i++) {
                    sumScore += scoreUses.get(i).getUseScore();
                    ilist.add(scoreUses.get(i).getUseId());
                    if(sumScore >= score){
                        tbUserScoreUse = scoreUses.get(i);
                        break;
                    }
                }
                /*删除所有使用过的积分*/
                scoreUseService.deleteAll(ilist);

                /*如果当前积分没有使用完成,重新添加一条*/
                Long useScore = sumScore - score;
                if (useScore >0){
                    TbUserScoreUse tb = new TbUserScoreUse();
                    tb.setSocreId(tbUserScoreUse.getSocreId());
                    tb.setUserId(tbUserScoreUse.getUserId());
                    tb.setAddtime(new Date());
                    tb.setClosetime(tbUserScoreUse.getClosetime());
                    tb.setUseScore(useScore);
                    scoreUseService.insertScore(tb);
                }
                result=1;
            }else{
                result=0;
            }
        }else{
            result=0;
        }
        return result;
    }
}
